package com.sysmind.ravi.margin.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.sysmind.ravi.margin.dao.CommonDao;
import com.sysmind.ravi.margin.dao.StockDao;
import com.sysmind.ravi.margin.model.Stock;
import com.sysmind.ravi.margin.utils.JDBCHelper;

public class StockDaoImpl implements StockDao 
{	Connection conn = null;
	PreparedStatement pstmt = null;
	Statement stmt = null;
	JDBCHelper jhelp = null;
	ResultSet rs = null;
	CommonDao commonDao = null;
	
	@Override
	public Stock getStockInfo(String stockSymbol) 
	{	jhelp = new JDBCHelper();
		conn = jhelp.getConnection();
	
		try
		{
			//conn.setAutoCommit(false);
			String sql = "Select * from stock where stock_symbol = \'"+stockSymbol.toUpperCase()+"\'";
			//System.out.println(sql);
			//pstmt = conn.prepareStatement(sql);
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			Stock stk = null;
			while(rs.next())
			{	stk = new Stock();
				stk.setStockId(rs.getInt("stock_id"));
				stk.setStockSymbol(rs.getString("stock_symbol"));
				stk.setStockPrice(rs.getDouble("stock_price"));
				stk.setStockTimeStamp(rs.getLong("stock_timestamp"));
				stk.setStockType(rs.getInt("stock_type"));				
			}
			return stk;
		}
		catch(SQLException se)
		{	
			se.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				commonDao = null;
				stmt.close();
				stmt = null;
				jhelp.closeConnection();
				jhelp = null;	
			}
			catch(Exception se)
			{
				se.printStackTrace();
			}
		}
		// TODO Auto-generated method stub
		//return null;
	}

	@Override
	public List<String> getStockSymbols() 
	{	commonDao = new CommonDaoImpl();
		
		if(commonDao.getRowCount("stock") == 0)
		{
			return null;
		}
		
		jhelp = new JDBCHelper();
		conn = jhelp.getConnection();
		List<String> stockSym;
		
		
		try
		{	//System.out.println("stockdaoImpl");
			stockSym = new ArrayList();
			conn.setAutoCommit(false);
			String sql = "Select stock_symbol as stocksym from stock";
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next())
			{	stockSym.add(rs.getString("stocksym"));
				//System.out.println(rs.getString("stocksym"));
			}
			return stockSym;
		}
		catch(SQLException se)
		{	
			se.printStackTrace();
			return null;
		}
		finally
		{
			try
			{	commonDao = null;
				stmt.close();
				stmt = null;
				jhelp.closeConnection();
				jhelp = null;
			}
			catch(Exception se)
			{
				se.printStackTrace();
			}
		}		
		// TODO Auto-generated method stub
		//return null;
	}

	@Override
	public boolean setStockInfo(List<Stock> stockList) 
	{	commonDao =  new CommonDaoImpl();
		if(commonDao.getRowCount("stock") != 0)
		{	
			commonDao.deleteAllRows("stock");
		}
		
		jhelp = new JDBCHelper();
		conn = jhelp.getConnection();
		
		try
		{	//System.out.println("stockdaoImpl");
			conn.setAutoCommit(false);
			String sql = "INSERT into stock VALUES(?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			for(Iterator<Stock> iterator = stockList.iterator();iterator.hasNext();)
			{	Object obj = iterator.next();
				if(obj instanceof Stock)
				{
					Stock stock = (Stock)obj;
					pstmt.setInt(1, stock.getStockId());
					pstmt.setString(2, stock.getStockSymbol());
					pstmt.setDouble(3, stock.getStockPrice());
					pstmt.setLong(4, stock.getStockTimeStamp());
					pstmt.setInt(5, stock.getStockType());
					pstmt.addBatch();
					//pstmt.
					//System.out.println("Stock object: "+stock);
				}									
			}
			//System.out.println("before batch update");
			int count[] = pstmt.executeBatch();
			//System.out.println("lavdeee... ");
			conn.commit();
			conn.setAutoCommit(true);
			
			return true;
		}
		catch(SQLException se)
		{	
			se.printStackTrace();
			return false;
		}
		finally
		{
			try
			{	commonDao = null;
				pstmt.close();
				pstmt = null;
				jhelp.closeConnection();
				jhelp = null;
			}
			catch(Exception se)
			{
				se.printStackTrace();
			}
		}		
	}
}
